package com.df.excel_export.validator.vadator_impl.type_array;

import com.df.excel_export.common.ExcelTable;
import com.df.excel_export.validator.AbstractIFieldValidator;
import com.df.excel_export.validator.IFieldValidator;
import com.df.excel_export.validator.IValidatorFactory;
import com.df.excel_export.validator.vadator_impl.type_string.StringLenRangeValidator;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArrayLenFixedValidator extends AbstractIFieldValidator implements IValidatorFactory {
    private int len;
    @Override
    public boolean support(String filedType, String ruleStr) {
        String regex = "^(int|long|float|string)\\[\\]$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(filedType);
        if(!matcher.find()) {
            return false;
        }
        if(ruleStr.startsWith("len(") && ruleStr.endsWith(")")) {
            return true;
        }
        return false;
    }

    @Override
    public IFieldValidator create(String filedType, String ruleStr) {
        ArrayLenFixedValidator validator  = new ArrayLenFixedValidator();
        String trim = ruleStr.trim();
        String newTrim = trim.substring(4, ruleStr.length() - 1);
        validator.len = Integer.parseInt(newTrim);
        if(validator.len<0){
            validator.len = 0;
        }
        return validator;
    }

    @Override
    public Integer getPriority() {
        return 0;
    }

    @Override
    public boolean validate(ExcelTable checkTable, List<Object> rowData, Integer dataRowIndex, Integer fieldIndex, String fieldName, String fieldTypeStr, Object filedValue) {
        String value = (String) filedValue;
        if(value.length()!=len ){
            return false;
        }
        return true;
    }

}
